<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-10-17
 * Time: 下午3:22
 */
namespace Common;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;

require_once dirname(__DIR__)."/ext/PHPMailer-master/src/PHPMailer.php";
require_once dirname(__DIR__)."/ext/PHPMailer-master/src/SMTP.php";

class SendEmail{

  /**
   * 发送邮件
   * @param string $address 收件人邮箱地址
   * @param string $content 邮件内容
   * @return bool
   */
  public function sendEmail(string $address,string $content):bool {
    $mail = new PHPMailer();
    //开启SMTP开启调试
    $mail->SMTPDebug = \MailConfig::SMTPDebug;
    //使用SMTP鉴权方式发送邮件
    $mail->isSMTP();
    //SMTP鉴权必须是true
    $mail->SMTPAuth = \MailConfig::SMTPAuth;
    // 链接qq域名邮箱的服务器地址
    $mail->Host = \MailConfig::Host;
    //设置使用ssl登录鉴权
    $mail->SMTPSecure = \MailConfig::SMTPSecure;
    //设置ssl连接smtp服务器的远程服务器端口号
    $mail->Port = \MailConfig::Port;
    $mail->CharSet = \MailConfig::CharSet;
    //设置发件人昵称
    $mail->FromName = \MailConfig::FromName;
    //设置SMTP登录的账号
    $mail->Username = \MailConfig::Username;
    //设置SMTP登录的密码 使用生成的授权码
    $mail->Password = \MailConfig::Password;
    //设置发件人邮箱地址
    $mail->From = \MailConfig::From;
    //邮件内容是否为HTML编码
    $mail->isHTML(\MailConfig::isHTML);
    //设置收件人邮箱地址 多个收件人添加多个方法即可
    $mail->addAddress($address);
    //设置邮件的主题
    $mail->Subject = \MailConfig::Subject;
    $mail->Body = "<h1>{$content}</h1>";
    try{
      $mail->addAttachment("/home/jacob/Pictures/Aurora.jpg");
      return $mail->send();
    }catch (Exception $e){
      Logger::getInstance()->info("mail attachment is failed",$e);
    }
    return false;
  }
}